package com.fc.happyteam.service.impl;

import com.fc.happyteam.dao.BaseDao;
import com.fc.happyteam.dao.RoleDao;
import com.fc.happyteam.entity.RoleEntity;
import com.fc.happyteam.service.IRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;

@Service("roleService")
public class RoleServiceImpl extends BaseServiceImpl<RoleEntity> implements IRoleService, Serializable {
	private RoleDao roleDao;

	@Autowired
	public BaseDao getDao(){
		return roleDao;
	}

	@Override
	public RoleEntity queryObject(Long roleId) {
		return roleDao.queryObject(roleId);
	}

	@Override
	@Transactional
	public void save(RoleEntity role) {
		roleDao.save(role);
		
		//检查权限是否越权
		checkPrems(role);
		
		//保存角色与菜单关系
//		ISysRoleMenuService.saveOrUpdate(role.getRoleId(), role.getMenuIdList());
	}

	@Override
	@Transactional
	public void update(RoleEntity role) {
		roleDao.update(role);
		
		//检查权限是否越权
		checkPrems(role);
		
		//更新角色与菜单关系
//		IRoleMenuService.saveOrUpdate(role.getRoleId(), role.getMenuIdList());
	}

	/**
	 * 检查权限是否越权
	 */
	private void checkPrems(RoleEntity role){
		//TODO
		//如果不是超级管理员，则需要判断角色的权限是否超过自己的权限
//		if(role.getCreateUserId() == Constant.SUPER_ADMIN){
//			return ;
//		}
		
		//查询用户所拥有的菜单列表
//		List<Long> menuIdList = userService.queryAllMenuId(role.getCreateUserId());
		
		//判断是否越权
//		if(!menuIdList.containsAll(role.getMenuIdList())){
//			throw new RRException("新增角色的权限，已超出你的权限范围");
//		}
	}
}
